import 'package:flutter/material.dart';

class DemoPage extends StatefulWidget {
  DemoPage({Key key}) : super(key: key);

  @override
  _DemoPageState createState() => _DemoPageState();
}

class _DemoPageState extends State<DemoPage> {
  var username;
  var sex = 1;
  List hobby = [
    {"checked": true, "title": "吃饭"},
    {"checked": false, "title": "睡觉"},
    {"checked": true, "title": "写代码"},
    {"checked": true, "title": "玩游戏"}
  ];
  List<Widget> setCheckedBox() {
    List<Widget> mList = new List();
    for (var i = 0; i < hobby.length; i++) {
      mList.add(Wrap(
        crossAxisAlignment: WrapCrossAlignment.center,
        children: <Widget>[
          Text(hobby[i]["title"] + ":"),
          Checkbox(
              value: this.hobby[i]["checked"],
              onChanged: (value) {
                setState(() {
                  this.hobby[i]["checked"] = value;
                });
              })
        ],
      ));
    }
    return mList;
  }

  void sexChanged(changedStuta) {
    setState(() {
      this.sex = changedStuta;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("checkBox_Radio实践页面"),
      ),
      body: Padding(
        padding: EdgeInsets.all(20),
        child: Column(
          children: <Widget>[
            TextField(
              decoration: InputDecoration(
                  hintText: "请输入用户信息", border: OutlineInputBorder()),
              onChanged: (v) {
                setState(() {
                  this.username = v;
                });
              },
            ),
            SizedBox(height: 10),
            Row(
              children: <Widget>[
                Text("男"),
                Radio(value: 1, groupValue: this.sex, onChanged: sexChanged),
                SizedBox(width: 20),
                Text("女"),
                Radio(value: 2, groupValue: this.sex, onChanged: sexChanged)
              ],
            ),
            //爱好
            Container(
                width: double.infinity,
                child: Wrap(
                  children: setCheckedBox(),
                )),
            SizedBox(height: 10),
            TextField(
              maxLines: 4, //几行
              decoration:
                  InputDecoration(hintText: "描述", border: OutlineInputBorder()),
              onChanged: (v) {
                setState(() {
                  this.username = v;
                });
              },
            ),
            SizedBox(height: 40),
            Container(
              width: double.infinity,
              height: 40,
              child: RaisedButton(
                child: Text("获取值"),
                onPressed: () {
                  print(this.username);
                  print(this.sex == 1 ? "男" : "女");
                  print(this.hobby);
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}
